package Test;

import Test.DateFormatException;

public class MyDate {
    public int year;
    public int month;
    public int day;
    public MyDate(int year, int month, int day){
        this.set(year,month,day);
    }
    public MyDate(String s) throws NumberFormatException, DateFormatException {
        if(s.isEmpty()){
            throw new DateFormatException("没有输入！");
        }
        int i = s.indexOf('年');
        int j = s.indexOf('月',i);
        int k = s.indexOf('日',j);
        int year = Integer.parseInt(s.substring(0,i));//[0,i)
        int month = Integer.parseInt(s.substring(i+1,j));//[i+1,j)
        int day = Integer.parseInt(s.substring(j+1,k));//[j+1,k)
        this.set(year,month,day);
    }

    public void set(int year, int month, int day){
        if(year<=2000 || year > 2023){
            throw new DateFormatException("年份不合适！");
        }
        if(month<1 || month >12){
            throw new DateFormatException("月份不合适！");
        }
        if(day<1 || day > 31){
            throw new DateFormatException("日期错误！");
        }
        this.year = year;
        this.day = day;
        this.month = month;
    }

    @Override
    public String toString() {
        return this.year + "年" +
                this.month + "月" +
                this.day + "日";
    }
}
